from pyspark import SparkConf, SparkContext

if __name__ == '__main__':
    # 构建SparkConf对象
    conf = SparkConf().setAppName("test").setMaster("local[*]")
    # 构建SparkContext执行环境入口对象
    sc = SparkContext(conf=conf)

    rdd = sc.parallelize([("a", 1), ("a", 1), ("a", 1), ("b", 1), ("b", 1)])

    result = rdd.countByKey()
    """
    countByKey: 根据key分组计数
    Action算子，返回类型为defaultdict
    
    """
    print(dict(result))
    # 输出结果 {'a': 3, 'b': 2}

    print(type(result))
    # 输出结果 <class 'collections.defaultdict'>